using System;
using System.Globalization;
using System.Linq;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OpenWaves.Web.Controls
{
    [ParseChildren(true)]
    public class ResponsiveBackgroundImagePanel : Control
    {
        public string ImageUrl { get; set; }

        public ImageTransformationType Transformation { get; set; }

        public string CssClass { get; set; }

        private readonly ImageBreakpointCollection breakpoints = new ImageBreakpointCollection();
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ImageBreakpointCollection Breakpoints
        {
            get { return this.breakpoints; }
        }

        private readonly PlaceHolder content = new PlaceHolder();
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public PlaceHolder Content
        {
            get { return this.content; }
        }

        protected override void OnInit(EventArgs e)
        {
            this.Controls.Add(this.Content);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (string.IsNullOrEmpty(this.ImageUrl))
                return;

            var transformedImageUrl = this.GetTransformedImageUrl();

            var js = new JavaScriptSerializer();
            var script = string.Format(CultureInfo.InvariantCulture, "ow.setResponsiveBackgroundImage({0});", js.Serialize(new
                {
                    elementId = this.ClientID, 
                    url = this.ResolveImageUrl(transformedImageUrl)
                }));

            this.Page.ClientScript.RegisterStartupScript(typeof(ResponsiveBackgroundImagePanel), this.UniqueID, script, true);
        }

        protected virtual string ResolveImageUrl(string url)
        {
            return this.ResolveUrl(url);
        }

        private string GetTransformedImageUrl()
        {
            var imageTransformationService = ServiceLocator.Resolve<IResponsiveWebImageTransformationService>();
            
            var transformations = this.Breakpoints.ToDictionary(
                breakpoint => breakpoint.Name, 
                breakpoint => (breakpoint.Transformation ?? this.Transformation).CreateImageTransformation(breakpoint.Width, breakpoint.Height));

            var transformedImageUrl = imageTransformationService.GetTransformedImageUrl(
                Url.Parse(this.ImageUrl),
                transformations);

            return transformedImageUrl.ToString();
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);

            if (string.IsNullOrEmpty(this.CssClass) == false)
                writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass);

            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            base.Render(writer);
            writer.RenderEndTag();
        }
    }
}